﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using System.Data;
using ncp.Business.Common;

namespace ncp.Web.cms
{
    public partial class System_Option : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(System_Option));
        }

        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable()
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            DataTable dt = SystemOptionBll.GetSystemOptionTable("", "", CommonConst.DB_Name);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                switch (dt.Rows[i]["Option_Name"].ToString())
                {
                    case "smtp_server":
                        jsonResult.Add("smtp_server", dt.Rows[i]["Option_Value"].ToString());
                        break;
                    case "smtp_username":
                        jsonResult.Add("smtp_username", dt.Rows[i]["Option_Value"].ToString());
                        break;
                    case "smtp_pwd":
                        jsonResult.Add("smtp_pwd", dt.Rows[i]["Option_Value"].ToString());
                        break;
                }
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //保存商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SaveSystemOption(string jsonData)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            Dictionary<string, object> DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            try
            {
                SystemOptionBll.UpdateSystemOption(DictList, CommonConst.DB_Name);
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "设置SMTP信息");
                dicHandleLog.Add("obj_guid", "");
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                jsonResult.Add("msg", "保存成功！");
                jsonResult.Add("isreload", true);

            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
